package com.xh.facerecognition.sdk;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.huawei.hms.mlsdk.livenessdetection.ui.LivenessDetectActivity;

public class FaceRecognition {

    private FaceRecognition.Callback callback;
    private FaceRecognitionConfig config = new FaceRecognitionConfig.Builder().build();

    private FaceRecognition() {
    }

    public static synchronized FaceRecognition getInstance() {
        return FaceRecognitionHolder.instance;
    }

    public void setConfig(FaceRecognitionConfig config) {
        this.config = config;
    }

    public FaceRecognitionConfig getConfig() {
        return this.config;
    }

    public void start(Activity activity, FaceRecognition.Callback callback) {
        this.callback = callback;
        if (TextUtils.isEmpty(config.getUrl())) {
            onFailure("url不能为空");
            return;
        }
        if (!config.getUrl().startsWith("http://") && !config.getUrl().startsWith("https://")) {
            onFailure("url不合法");
            return;
        }
        activity.startActivity(new Intent(activity, FaceRecognitionActivity.class));
    }

    public void onSuccess(String idcard) {
        FaceRecognition.Callback callback;
        if ((callback = this.callback) != null) {
            callback.onSuccess(idcard);
        }
    }

    public void onFailure(String errorMsg) {
        FaceRecognition.Callback callback;
        if ((callback = this.callback) != null) {
            callback.onFailure(errorMsg);
        }
    }

    public interface Callback {
        void onSuccess(String idcard);

        void onFailure(String errorMsg);
    }

    private static class FaceRecognitionHolder {
        private static final FaceRecognition instance = new FaceRecognition();
    }
}
